﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;
using System.Net;
using System.Net.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography.X509Certificates;


//using ICSharpCode.SharpZipLib.GZip;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        FileStream fs = new FileStream("n:\\InsureTest\\policyCert.xml", FileMode.Open, FileAccess.Read, FileShare.Read);

        //FileStream fs = new FileStream("f:\\InsureTest\\print.xml", FileMode.Open, FileAccess.Read, FileShare.Read);

        //FileStream fs = new FileStream("f:\\InsureTest\\revokePolicy.xml", FileMode.Open, FileAccess.Read, FileShare.Read);

        byte[] date = new byte[fs.Length];
        fs.Read(date, 0, date.Length);

        fs.Close();




        HttpRequest myReuqest = new HttpRequest();
        //myReuqest.__Uri__ = "https://eairiis-intdmz.paic.com.cn/invoke/wm.tn/receive";
        //测试环境
        myReuqest.__Uri__ = "https://eairiis-stgdmz.paic.com.cn/invoke/wm.tn/receive";
        //真实环境
        //myReuqest.__Uri__ = "https://eairiis-prddmz.paic.com.cn/invoke/wm.tn/receive";
        myReuqest.__Method__ = "POST";
        myReuqest.mybytes = date;
        //Response.Write(myReuqest.OpenRequest());
        string ret = myReuqest.OpenRequest();
        Response.ContentType = "text/xml";
        
        //Response.Write(ret);
        Response.Write(myReuqest.HtmlDocument);
    }



    /// <summary>
    /// HTTP/HTTPS资源的请求。
    /// </summary>
    public class HttpRequest
    {
        // 保持Cookie为同一Cookie值。        
        protected CookieContainer craboCookie = new CookieContainer();

        public string __Uri__ = null;        // 标识 Internet 资源的 URI

        public string __Referer__ = null;        // 标识 Internet 资源请求的 Referer        

        public string __Headers__ = null;        // 标识 Internet 资源请求的 Header

        // 标识 Internet 资源请求的 Accept
        public string __Accept__ = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";

        public string __Method__ = null;        // 标识 Internet 资源请求的 Method

        public string __Data__ = null;        // POST请求时的数据

        public byte[] mybytes = null;

        public string __CharacterSet__ = null;        // 响应的字符集

        public HttpStatusCode __StatusCode__;        // 响应状态

        public StringBuilder __Html_Text__ = new StringBuilder();

        public HttpRequest()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            __CharacterSet__ = "GB2312";
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
        }

        private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            //Return True to force the certificate to be accepted. 
            //just like pressing the OK button in the Browser
            return true;
        }



        /// <summary>
        /// 对 Internet 资源发起请求
        /// </summary>
        /// <returns></returns>
        public string OpenRequest()
        {
            // 清空已保留代码
            __Html_Text__.Remove(0, __Html_Text__.Length);

            // 创建 HttpWebRequest 实例
            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(__Uri__);

            // 设置跟随重定向
            Request.AllowAutoRedirect = true;




            string certPath2 = "F:\\InsureTest\\PAICSTG.cer";
            //生产证书
            //string certPath3 = "F:\\InsureTest\\test.pfx";
            //测试证书
            string certPath3 = "n:\\InsureTest\\stg－cer.pfx";
            string certPath4 = "F:\\InsureTest\\PAICROOTSTG.cer";






            //Request.ClientCertificates.Add(new X509Certificate2(certPath2));
            //Request.ClientCertificates.Add(new X509Certificate2(certPath4)); 

            //生产证书密码
            //Request.ClientCertificates.Add(new X509Certificate2(certPath3, "123456"));
            //测试证书密码
            Request.ClientCertificates.Add(new X509Certificate2(certPath3, "panjin"));



            //Request.KeepAlive = true;

            Request.Timeout = 10000;
            Request.Method = __Method__;
            Request.Accept = __Accept__;
            Request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322); Http STdns";
            Request.CookieContainer = craboCookie;
            Request.Referer = __Referer__;
            Request.Method = __Method__;
            if (__Method__ == "POST")
            {
                #region 请求为POST
                //Request.ContentType = "application/x-www-form-urlencoded";
                Request.ContentType = "text/xml";
                //byte[] Bytes = Encoding.GetEncoding(CharacterSet).GetBytes(__Data__);
                byte[] Bytes = mybytes;
                Request.ContentLength = Bytes.Length;

                try
                {
                    //发送xml
                    using (Stream writer = Request.GetRequestStream())
                    {
                        writer.Write(Bytes, 0, Bytes.Length);
                        writer.Close();


                    }

                }
                catch (Exception err)
                {

                    return err.Message + "请求过程错误";
                }

                try
                {
                    using (Stream reader = Request.GetResponse().GetResponseStream())
                    {
                        StreamReader sr = new StreamReader(reader, Encoding.GetEncoding(CharacterSet));
                        __Html_Text__.Append(sr.ReadToEnd());
                        sr.Close();
                        reader.Close();

                    }
                }
                catch (Exception err)
                {

                    return err.Message + "接受过程错误";
                }

                string msg = Request.GetResponse().ResponseUri.ToString();
                Request.Abort();
                return msg;


                #endregion
            }


            bool bResult = true;






            return "错误不明";
        }

        /// <summary>
        /// 获取或设置 Uri 资源字符集
        /// </summary>
        public string CharacterSet
        {
            get
            {
                return __CharacterSet__;
            }
            set
            {
                __CharacterSet__ = value;
            }
        }

        /// <summary>
        /// 获取或设置 Uri 资源标识
        /// </summary>
        public string RequestUriString
        {
            get
            {
                return __Uri__;
            }
            set
            {
                __Uri__ = value;
            }
        }

        /// <summary>
        /// 获取或设置 Uri 资源请求 Accept
        /// </summary>
        public string Accept
        {
            get
            {
                return __Accept__;
            }
            set
            {
                __Accept__ = value;
            }
        }

        public string HtmlDocument
        {
            get
            {
                return __Html_Text__.ToString();
            }
        }
    }

    /// <summary>
    /// 当 Uri 资源为 HTTPS 时，忽略证书。
    /// </summary>



}

public class MyPolicy : ICertificatePolicy
{
    public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
    {
        //Return True to force the certificate to be accepted.
        return true;
    }
}

